//
//  YSFallDownEmitterView.m
//  FullDownDemo
//
//  Created by YangShuai on 16/11/12.
//  Copyright © 2016年 ys. All rights reserved.
//

#import "YSFallDownEmitterView.h"

@interface YSFallDownEmitterView ()
@property(nonatomic,strong)CAEmitterLayer* fallDownEmitterLayer;
@end

@implementation YSFallDownEmitterView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
//
+(YSFallDownEmitterView*)showFallDowmViewWithFrame:(CGRect)frame FallImage:(UIImage*)fallImage{
    YSFallDownEmitterView* fallDownView = [[self alloc] initWithFrame:frame];
    [fallDownView addFallDownEffectWithImage:fallImage];
    return fallDownView;
}

//
-(void)addFallDownEffectWithImage:(UIImage*)image{
    self.fallDownEmitterLayer = [CAEmitterLayer layer];
    _fallDownEmitterLayer.emitterPosition = CGPointMake(self.frame.size.width / 2, -20);
    //粒子发射范围
    _fallDownEmitterLayer.emitterSize = CGSizeMake(self.frame.size.width , 0);
    //发射模式
    _fallDownEmitterLayer.emitterMode = kCAEmitterLayerSurface;
    //粒子模式
    _fallDownEmitterLayer.emitterShape = kCAEmitterLayerLine;
    //
    //创建粒子
    CAEmitterCell* emitterCell = [CAEmitterCell emitterCell];
    //设置粒子内容
    emitterCell.contents = (__bridge id)image.CGImage;
    
    //设置粒子缩放比例
    emitterCell.scale = 0.2;
    //缩放容差
    emitterCell.scaleRange = 0.2;
    //每秒产生粒子的数量
    emitterCell.birthRate = 20.f;
    //粒子生命周期
    emitterCell.lifetime = 80.f;//80;
    //粒子速度
    emitterCell.velocity = 200.f;
    //速度容差
    emitterCell.velocityRange = 10.f;
    //y轴加速度
    emitterCell.yAcceleration = 30.f;
    //设置发射角度容差
    emitterCell.emissionRange = 0;
    //xy平面上发射角度
    emitterCell.emissionLongitude = M_PI;


    //设置粒子旋转角速度
    emitterCell.spin = M_PI_4;
    //
    //设置layer阴影
    //_fallDownEmitterLayer.shadowOpacity = 1.0;
    //设置圆角
    //_fallDownEmitterLayer.shadowRadius = 8;
    //设置偏移
    //_fallDownEmitterLayer.shadowOffset = CGSizeMake(3, 3);
    //设置阴影颜色
    //_fallDownEmitterLayer.shadowColor = [UIColor whiteColor].CGColor;
    //设置layer的粒子
    _fallDownEmitterLayer.emitterCells = @[emitterCell];
    [self.layer addSublayer:_fallDownEmitterLayer];
}

+(YSFallDownEmitterView*)showFallDownSpatterViewFrame:(CGRect)frame FallImageArray:(NSArray*)fallImageArray{
    YSFallDownEmitterView* fallDownView = [[self alloc] initWithFrame:frame];
    [fallDownView addFallDownSpatterEffectWithImageArray:fallImageArray];
    return fallDownView;
}
//
-(void)addFallDownSpatterEffectWithImageArray:(NSArray*)imageArray{

    //粒子发射器图层
    self.fallDownEmitterLayer=[CAEmitterLayer layer];
    
    _fallDownEmitterLayer.emitterPosition=CGPointMake(100, -30);
    
    //粒子发射器的范围
    _fallDownEmitterLayer.emitterSize=CGSizeMake(self.bounds.size.width*2, 0);
    
    //发射模式
    _fallDownEmitterLayer.emitterMode=kCAEmitterLayerOutline;
    
    //粒子模式
    _fallDownEmitterLayer.emitterShape=kCAEmitterLayerLine;
    NSMutableArray* cellArray = [NSMutableArray new];
    for (UIImage* image in imageArray) {
        //创建粒子
        CAEmitterCell *emitterCell=[CAEmitterCell emitterCell];
        //设置粒子内容
        emitterCell.contents=(__bridge id)(image.CGImage);
        //设置粒子缩放比例
        emitterCell.scale = 0.5;
        //缩放范围
        emitterCell.scaleRange = 1;
        //每秒粒子产生数量
        emitterCell.birthRate = 20;
        //粒子生命周期
        emitterCell.lifetime = 10;
        //粒子透明速度
        emitterCell.alphaSpeed = -0.01;
        //粒子速度
        emitterCell.velocity = 80;
        emitterCell.velocityRange = 50;
        //设置角度
        emitterCell.emissionRange=M_PI;
        
        //设置粒子旋转角速度
        emitterCell.spin=M_PI_4;
        [cellArray addObject:emitterCell];
    }
    //设置layer阴影
    //_fallDownEmitterLayer.shadowOpacity=1.0;
    
    //设置圆角
    //_fallDownEmitterLayer.shadowRadius=8;
    
    //设置偏移
    //_fallDownEmitterLayer.shadowOffset=CGSizeMake(3, 3);
    
    //设置颜色
    //_fallDownEmitterLayer.shadowColor=[UIColor whiteColor].CGColor;
    //设置layer的粒子
    _fallDownEmitterLayer.emitterCells = cellArray;
    
    [self.layer addSublayer:_fallDownEmitterLayer];
}

+(YSFallDownEmitterView*)showSparkViewFrame:(CGRect)frame FallImageArray:(NSArray*)fallImageArray layerPosition:(CGPoint)position{
    YSFallDownEmitterView* fallDownView = [[self alloc] initWithFrame:frame];
    [fallDownView addSparkEffectWithImageArray:fallImageArray emitterPosition:position];
    return fallDownView;
}

- (void)addSparkEffectWithImageArray:(NSArray*)imageArray emitterPosition:(CGPoint)pisition{
    
    //粒子发射器图层
    self.fallDownEmitterLayer=[CAEmitterLayer layer];
    _fallDownEmitterLayer.emitterPosition=pisition;
    
    //粒子发射器的范围
    _fallDownEmitterLayer.emitterSize=CGSizeMake(self.bounds.size.width*1.5, 0);
    
    //发射模式
    _fallDownEmitterLayer.emitterMode=kCAEmitterLayerOutline;
    
    //粒子模式
    _fallDownEmitterLayer.emitterShape=kCAEmitterLayerLine;
    NSMutableArray* cellArray = [NSMutableArray new];
    for (UIImage* image in imageArray) {
        //创建粒子
        CAEmitterCell *emitterCell=[CAEmitterCell emitterCell];
        //设置粒子内容
        emitterCell.contents=(__bridge id)(image.CGImage);
        //设置粒子缩放比例
        emitterCell.scale = 0.5;
        //缩放范围
        emitterCell.scaleRange = 1;
        //每秒粒子产生数量
        emitterCell.birthRate = 20;
        //粒子生命周期
        emitterCell.lifetime = 10;
        //粒子透明速度
        emitterCell.alphaSpeed = -0.01;
        //粒子速度
        emitterCell.velocity = 90;
        emitterCell.velocityRange = 50;
        //设置角度
        emitterCell.emissionRange=M_PI;
        
        //设置粒子旋转角速度
        emitterCell.spin=M_PI_4;
        [cellArray addObject:emitterCell];
    }
    //设置layer阴影
    //_fallDownEmitterLayer.shadowOpacity=1.0;
    
    //设置圆角
    //_fallDownEmitterLayer.shadowRadius=8;
    
    //设置偏移
    //_fallDownEmitterLayer.shadowOffset=CGSizeMake(3, 3);
    
    //设置颜色
    //_fallDownEmitterLayer.shadowColor=[UIColor whiteColor].CGColor;
    //设置layer的粒子
    _fallDownEmitterLayer.emitterCells = cellArray;
    
    [self.layer addSublayer:_fallDownEmitterLayer];
    
    /*
    _fallDownEmitterLayer = [CAEmitterLayer layer];
    CGRect viewBounds = self.layer.bounds;
    _fallDownEmitterLayer.emitterPosition = CGPointMake(self.frame.size.width/2, viewBounds.size.height/2-50);
    _fallDownEmitterLayer.emitterSize	= CGSizeMake(10, 10.0);
    _fallDownEmitterLayer.emitterMode	= kCAEmitterLayerVolume;
    _fallDownEmitterLayer.emitterShape	= kCAEmitterLayerRectangle;
    _fallDownEmitterLayer.renderMode		= kCAEmitterLayerAdditive;
    
    NSMutableArray* cellArray = [NSMutableArray new];
    
    for (UIImage* image in imageArray) {
        CAEmitterCell* spark = [CAEmitterCell emitterCell];
        spark.name = @"spark";
        spark.emissionRange = 2*M_PI;
        spark.lifetime = 10.0;
        spark.velocity = -100.0;
        spark.velocityRange = 60.0;
        spark.yAcceleration = 80.0;
        spark.contents = (id)image.CGImage;
        //spark.scale	= 0.15;
        //spark.scaleSpeed = 0.5;
        spark.spinRange	= 2.0 * M_PI;
        spark.birthRate	= 20;
        [cellArray addObject:spark];
    }

    _fallDownEmitterLayer.emitterCells				= cellArray;
    [self.layer addSublayer:_fallDownEmitterLayer];
    
//    CABasicAnimation *heartsBurst = [CABasicAnimation animationWithKeyPath:@"emitterCells.spark.birthRate"];
//    heartsBurst.fromValue		= [NSNumber numberWithFloat:100.0];
//    heartsBurst.toValue			= [NSNumber numberWithFloat:  0.0];
//    heartsBurst.duration		= 3.0;
//    heartsBurst.timingFunction	= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//    [_fallDownEmitterLayer addAnimation:heartsBurst forKey:@"heartsBurst"];
     */
}

@end
